package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisMessageConstant;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Member;
import com.atguigu.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    JedisPool jedisPool;
    @Reference
    MemberService memberService;
    //登录 1.检查是不是会员 2.比较验证码
    @RequestMapping("/check.do")
    public Result check(@RequestBody Map map){
        //获取手机号
        String telephone= map.get("telephone").toString();
        Member member= memberService.getMemberByTel(telephone);
        if(member==null){
            member=new Member();
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberService.add(member);
        }

        //获取验证码
        String code= map.get("validateCode").toString();
        //获取redis验证码
        String redisCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        if(redisCode==null || !redisCode.equals(code)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }


        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

}
